package org.szgb.console.ht.meter.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.sys.line.entity.SysLineLocation;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class HtMeterReadingDaily extends BeanEntity<HtMeterReadingDaily> {
    @Serial
    private static final long serialVersionUID = 1L;
    private HtMeterReadingTask task;
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate date;
    private BigDecimal value;
    private String pictureId;
    private Boolean isApproval;

    public HtMeterReadingDaily() {
    }

    public HtMeterReadingDaily(String id) {
        this.setId(id);
    }

    public HtMeterReadingDaily(HtMeterReadingTask task, LocalDate date) {
        this.setTask(task);
        this.setDate(date);
    }

    public HtMeterReadingDaily(HtMeterReadingDaily.HtMeterReadingDailyFindDto htMeterReadingDailyFindDto) {
        this.setId(htMeterReadingDailyFindDto.getId());
        HtMeterReadingTask task = new HtMeterReadingTask();
        if (htMeterReadingDailyFindDto.getTaskId() != null) {
            task.setId(htMeterReadingDailyFindDto.getTaskId());
        }
        if (htMeterReadingDailyFindDto.getLocationId() != null) {
            task.setLocation(new SysLineLocation(htMeterReadingDailyFindDto.getLocationId()));
        }
        this.setTask(task);
        this.setDate(htMeterReadingDailyFindDto.getDate());
        this.setIsApproval(htMeterReadingDailyFindDto.getIsApproval());
        this.setPageDto(htMeterReadingDailyFindDto.getPageDto());
    }

    public HtMeterReadingDaily(HtMeterReadingDaily.HtMeterReadingDailySaveDto htMeterReadingDailySaveDto) {
        this.setId(htMeterReadingDailySaveDto.id);
        this.setTask(new HtMeterReadingTask(htMeterReadingDailySaveDto.taskId));
        this.setDate(htMeterReadingDailySaveDto.getDate());
        this.setValue(htMeterReadingDailySaveDto.getValue());
        this.setPictureId(htMeterReadingDailySaveDto.getPictureId());
    }

    public HtMeterReadingDaily(HtMeterReadingDaily.HtMeterReadingDailyBatchSaveDto htMeterReadingDailyBatchSaveDto) {
        this.setTask(new HtMeterReadingTask(htMeterReadingDailyBatchSaveDto.taskId));
        this.setValue(htMeterReadingDailyBatchSaveDto.getValue());
        this.setPictureId(htMeterReadingDailyBatchSaveDto.getPictureId());
    }

    public HtMeterReadingDaily(HtMeterReadingDaily.HtMeterReadingDailyApprovalSaveDto htMeterReadingDailyApprovalSaveDto) {
        this.setId(htMeterReadingDailyApprovalSaveDto.getId());
        this.setValue(htMeterReadingDailyApprovalSaveDto.getValue());
        this.setIsApproval(true);
    }

    public HtMeterReadingDaily.HtMeterReadingDailyView toHtMeterReadingDailyView() {
        return new HtMeterReadingDaily.HtMeterReadingDailyView(this);
    }

    @Data
    @Schema(title = "日常表计记录信息", description = "日常表计记录信息")
    public static class HtMeterReadingDailyView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "任务id")
        private String taskId;
        @Schema(description = "任务名称")
        private String taskName;
        @Schema(description = "地点名称")
        private String locationName;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "日期(格式yyyy-MM-dd)")
        private LocalDate date;
        @Schema(description = "数值")
        private BigDecimal value;
        @Schema(description = "图片id")
        private String pictureId;
        @Schema(description = "是否审核")
        private Boolean isApproval;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public HtMeterReadingDailyView(HtMeterReadingDaily htMeterReadingDaily) {
            this.id = htMeterReadingDaily.getId();
            this.taskId = htMeterReadingDaily.getTask().getId();
            this.taskName = htMeterReadingDaily.getTask().getName();
            this.locationName = htMeterReadingDaily.getTask().getLocation().getName();
            this.date = htMeterReadingDaily.getDate();
            this.value = htMeterReadingDaily.getValue();
            this.pictureId = htMeterReadingDaily.getPictureId();
            this.isApproval = htMeterReadingDaily.getIsApproval();
            this.createdAt = htMeterReadingDaily.getCreatedAt();
            this.updatedAt = htMeterReadingDaily.getUpdatedAt();
            if (htMeterReadingDaily.getCreatedBy() != null) {
                this.createdById = htMeterReadingDaily.getCreatedBy().getId();
                this.createdByName = htMeterReadingDaily.getCreatedBy().getName();
            }
            if (htMeterReadingDaily.getUpdatedBy() != null) {
                this.updatedById = htMeterReadingDaily.getUpdatedBy().getId();
                this.updatedByName = htMeterReadingDaily.getUpdatedBy().getName();
            }
        }

        public static List<HtMeterReadingDaily.HtMeterReadingDailyView> getViewListFromList(List<HtMeterReadingDaily> htMeterReadingDailyList) {
            return htMeterReadingDailyList.stream().map(HtMeterReadingDaily.HtMeterReadingDailyView::new).toList();
        }
    }

    @Data
    @Schema(title = "日常表计记录查询表单", description = "日常表计记录查询参数")
    public static class HtMeterReadingDailyFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "地点id")
        private String LocationId;
        @Schema(description = "任务id")
        private String taskId;
        @Schema(description = "是否审核")
        private Boolean isApproval;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "日期(格式yyyy-MM-dd)")
        private LocalDate date;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public HtMeterReadingDaily toHtMeterReadingDaily() {
            return new HtMeterReadingDaily(this);
        }
    }

    @Data
    @Schema(title = "日常表计记录保存表单", description = "日常表计记录保存参数")
    public static class HtMeterReadingDailySaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "任务id")
        private String taskId;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "日期(格式yyyy-MM-dd)")
        private LocalDate date;
        @Schema(description = "数值")
        private BigDecimal value;
        @Schema(description = "图片id")
        private String pictureId;

        public HtMeterReadingDaily toHtMeterReadingDaily() {
            return new HtMeterReadingDaily(this);
        }
    }

    @Data
    @Schema(title = "日常表计记录批量保存表单", description = "日常表计记录批量保存参数")
    public static class HtMeterReadingDailyBatchSaveDto {
        @Schema(description = "任务id")
        private String taskId;
        @Schema(description = "数值")
        private BigDecimal value;
        @Schema(description = "图片id")
        private String pictureId;

        public HtMeterReadingDaily toHtMeterReadingDaily() {
            return new HtMeterReadingDaily(this);
        }
    }

    @Data
    @Schema(title = "日常表计记录审批保存表单", description = "日常表计记录审批保存参数")
    public static class HtMeterReadingDailyApprovalSaveDto {
        @Schema(description = "id")
        private String id;
        @Schema(description = "数值")
        private BigDecimal value;

        public HtMeterReadingDaily toHtMeterReadingDaily() {
            return new HtMeterReadingDaily(this);
        }
    }
}
